﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Xml;
using System.IO;
using System.Xml.Linq;

namespace MealBooking.Core.Utils
{
    public static class Utils
    {
        /// <summary>
        /// app_data root directory.
        /// </summary>
        /// <returns></returns>
        public static string StorageLocation()
        {
            string path;
            if (String.IsNullOrEmpty(System.Web.Configuration.WebConfigurationManager.AppSettings["StorageLocation"]))
                path = @"~/app_data/";
            else
            {
                path = System.Web.Configuration.WebConfigurationManager.AppSettings["StorageLocation"];
            }
            path = path.Replace("~/", "");
            return System.IO.Path.Combine(System.Web.HttpRuntime.AppDomainAppPath, path);
        }

        public static XDocument InitXmlFile(string FilePath)
        {
            if (!File.Exists(FilePath))
            {
                XDocument doc = new XDocument(new XDeclaration("1.0", "utf-8", "yes"));
                doc.AddFirst(new XElement("Root"));
                doc.Save(FilePath);
            }
            return XDocument.Load(FilePath);
        }

        public static void SaveFile(object locker, XDocument doc, string filePath)
        {
            lock (locker)
            {
                doc.Save(filePath);
            }
        }
    }
}